package com.styp152.Android.File.TakeNote;

import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.ArrayList;

import android.app.ListActivity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;

public class TakeNote extends ListActivity {
	TextView selection;
	private Toast notificacionToast;
	Button btnCreate;
	ArrayList<String> items = new ArrayList<String>();
	private final static String NOTES = "notes.txt";

	/** Called when the activity is first created. */
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.main);
		btnCreate = (Button) findViewById(R.id.create);

		btnCreate.setOnClickListener(new Button.OnClickListener() {
			public void onClick(View v) {
				try {
					Intent startActivity = new Intent(TakeNote.this,
							Editor.class);
					startActivity(startActivity);
				} catch (Exception e) {
					notificacionToast = Toast.makeText(getApplicationContext(),
							"Exc " + e, Toast.LENGTH_SHORT);
					notificacionToast.show();
				}
			}

		});
		selection = (TextView) findViewById(R.id.selection);
		try {
			InputStream in = openFileInput(NOTES);
			if (in != null) {
				InputStreamReader tmp = new InputStreamReader(in);
				BufferedReader reader = new BufferedReader(tmp);
				String str;
				StringBuffer buf = new StringBuffer();
				while ((str = reader.readLine()) != null) {
					buf.append(str + "\n");
					if(buf.toString().contains(".\n")){
						
//						buf.delete(0, buf.toString().indexOf(".\n"));
					}
				}
				in.close();
				items.add(buf.toString());//.substring(0, buf.toString().indexOf(".\n"))
			}
		} catch (java.io.FileNotFoundException e) {
			// that's OK, we probably haven't created it yet
		} catch (Throwable t) {
			Toast.makeText(this, "Exception: " + t.toString(), 2000).show();
			selection.setText(R.string.not_notes);
		}
		
		setListAdapter(new ArrayAdapter<String>(this,
				android.R.layout.simple_list_item_1, items));
	}
	public void onResume() {
		super.onResume();
		try {
			InputStream in = openFileInput(NOTES);
			if (in != null) {
				items.clear();
				InputStreamReader tmp = new InputStreamReader(in);
				BufferedReader reader = new BufferedReader(tmp);
				String str;
				StringBuffer buf = new StringBuffer();
				while ((str = reader.readLine()) != null) {
					buf.append(str + "\n");
					if(buf.toString().contains(".\n")){
						items.add(buf.toString().substring(0, buf.toString().indexOf(".\n")+1));//
						buf.delete(0, buf.toString().indexOf(".\n")+1);
					}
				}
				if(buf.length()!=0){
					items.add(buf.toString());
				}
				in.close();
			}
		} catch (java.io.FileNotFoundException e) {
			// that's OK, we probably haven't created it yet
		} catch (Throwable t) {
			Toast.makeText(this, "Exception: " + t.toString(), 2000).show();
		}
		setListAdapter(new ArrayAdapter<String>(this,
				android.R.layout.simple_list_item_1, items));
	}
}